package filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created by v on 17-4-11.
 */
@WebFilter(filterName = "filter",value = "/index.jsp",initParams = {
        @WebInitParam(name = "count",value = "5000")
})
public class TestFilter implements Filter{
    private int count;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        String param=filterConfig.getInitParameter("count");
        count=Integer.valueOf(param);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        count++;
        HttpServletRequest req=(HttpServletRequest)servletRequest;

        req.getSession().getServletContext().setAttribute("count",count);

        filterChain.doFilter(servletRequest,servletResponse);
    }
}
